package com.cyt.work.repair.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.repair.dao.AssetWarrantyDao;
import com.cyt.work.repair.model.po.AssetWarranty;
import com.cyt.work.repair.model.vo.AssetWarrantyVo;
import com.cyt.work.repair.service.AssetWarrantyService;
import com.cyt.wx.model.po.WxMember;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 资产报修-资产报修Service层实现类
 *
 * @author han jun
 * @date 2019-7-8 15:03:04
 */
@Service("assetWarrantyService")
public class AssetWarrantyServiceImpl extends BaseServiceImpl<AssetWarranty> implements AssetWarrantyService {

    @Resource
    private AssetWarrantyDao assetWarrantyDao;

    @Autowired
    public void setBaseDao(AssetWarrantyDao assetWarrantyDao) {
        this.baseDao = assetWarrantyDao;
    }

    /**
     * 分页查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @param pageNum         分页数
     * @param pageSize        每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public ReturnResult findPage(AssetWarrantyVo assetWarrantyVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(AssetWarranty.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);

        List<AssetWarranty> list = assetWarrantyDao.selectByExample(example);
        PageInfo<AssetWarranty> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @param pageNum         分页数
     * @param pageSize        每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public ReturnResult findVoPage(AssetWarrantyVo assetWarrantyVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AssetWarrantyVo> list = assetWarrantyDao.selectVoList(assetWarrantyVo);
        PageInfo<AssetWarrantyVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @return List<AssetWarranty>
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public List<AssetWarranty> findList(AssetWarrantyVo assetWarrantyVo) {
        Example example = new Example(AssetWarranty.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        if (StrUtil.isNotEmpty(assetWarrantyVo.getTitle())) {
            criteria.andLike("title", "%" + assetWarrantyVo.getTitle() + "%");
        }
        if (StrUtil.isNotEmpty(assetWarrantyVo.getAssetState())) {
            criteria.andEqualTo("assetState", assetWarrantyVo.getAssetState());
        }
        if (StringUtils.isNotBlank(assetWarrantyVo.getApplyId())) {
            criteria.andEqualTo("applyId", assetWarrantyVo.getApplyId());
        }
        if (StringUtils.isNotBlank(assetWarrantyVo.getCreateTime())) {
            criteria.andCondition("date_format(create_time, '%Y-%m') = '" + assetWarrantyVo.getCreateTime() + "'");
        }
        return assetWarrantyDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @return List<AssetWarranty>
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public List<AssetWarrantyVo> findVoList(AssetWarrantyVo assetWarrantyVo) {
        return assetWarrantyDao.selectVoList(assetWarrantyVo);
    }

    /**
     * 单个对象查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @return AssetWarranty
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public AssetWarranty findOne(AssetWarrantyVo assetWarrantyVo) {
        AssetWarranty assetWarranty = new AssetWarranty();
        if (StrUtil.isEmpty(assetWarrantyVo.getId())) {
            assetWarranty.setId(assetWarrantyVo.getId());
        }
        return assetWarrantyDao.selectOne(assetWarranty);
    }

    /**
     * 单个对象查询
     *
     * @param assetWarrantyVo 接受参数对象
     * @return AssetWarranty
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    public AssetWarrantyVo findVoOne(AssetWarrantyVo assetWarrantyVo) {
        return assetWarrantyDao.selectVoOne(assetWarrantyVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdateNew(AssetWarrantyVo assetWarrantyVo, SysUser currentUser) {
        AssetWarranty assetWarranty;
        //初始化对象
        if (StrUtil.isEmpty(assetWarrantyVo.getId())) {
            assetWarranty = new AssetWarranty();
            assetWarranty.setCreateTime(new Date());
            assetWarranty.setCreateUser(currentUser.getId());
            assetWarranty.setUpdateUser(currentUser.getId());
            assetWarranty.setUpdateTime(new Date());
        } else {
            assetWarranty = assetWarrantyDao.selectByPrimaryKey(assetWarrantyVo.getId());
        }
        //设置属性值
        assetWarranty.setApplyId(assetWarrantyVo.getApplyId());
        assetWarranty.setUserId(currentUser.getId());
        assetWarranty.setOrganName(assetWarrantyVo.getOrganName());
        assetWarranty.setMobile(assetWarrantyVo.getMobile());
        assetWarranty.setAssetType(assetWarrantyVo.getAssetType());
        assetWarranty.setTitle(assetWarrantyVo.getTitle());
        assetWarranty.setLocation(assetWarrantyVo.getLocation());
        assetWarranty.setDescription(assetWarrantyVo.getDescription());
        assetWarranty.setObjectId(assetWarrantyVo.getObjectId());
        assetWarranty.setAssetState(assetWarrantyVo.getAssetState());
        //提交数据库
        if (StrUtil.isEmpty(assetWarrantyVo.getId())) {
            assetWarrantyDao.insert(assetWarranty);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            assetWarranty.setUpdateTime(new Date());
            assetWarranty.setUpdateUser(currentUser.getId());
            assetWarrantyDao.updateByPrimaryKey(assetWarranty);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 添加或更新
     *
     * @param assetWarrantyVo 参数接受对象
     * @param wxMember        当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AssetWarrantyVo assetWarrantyVo, WxMember wxMember) {
        AssetWarranty assetWarranty;
        //初始化对象
        if (StrUtil.isEmpty(assetWarrantyVo.getId())) {
            assetWarranty = new AssetWarranty(wxMember);
        } else {
            assetWarranty = assetWarrantyDao.selectByPrimaryKey(assetWarrantyVo.getId());
        }
        //设置属性值
        assetWarranty.setApplyId(assetWarrantyVo.getApplyId());
        assetWarranty.setUserId(wxMember.getId());
        assetWarranty.setOrganName(assetWarrantyVo.getOrganName());
        assetWarranty.setMobile(assetWarrantyVo.getMobile());
        assetWarranty.setAssetType(assetWarrantyVo.getAssetType());
        assetWarranty.setTitle(assetWarrantyVo.getTitle());
        assetWarranty.setLocation(assetWarrantyVo.getLocation());
        assetWarranty.setDescription(assetWarrantyVo.getDescription());
        assetWarranty.setObjectId(assetWarrantyVo.getObjectId());
        assetWarranty.setAssetState(assetWarrantyVo.getAssetState());
        //提交数据库
        if (StrUtil.isEmpty(assetWarrantyVo.getId())) {
            assetWarrantyDao.insert(assetWarranty);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            assetWarranty.setUpdateTime(new Date());
            assetWarranty.setUpdateUser(wxMember.getId());
            assetWarrantyDao.updateByPrimaryKey(assetWarranty);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param assetWarrantyVo 参数接受对象
     * @param currentUser     当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AssetWarrantyVo assetWarrantyVo, SysUser currentUser) {
        //修改字段
        AssetWarranty assetWarranty = new AssetWarranty();
        assetWarranty.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        assetWarranty.setUpdateUser(currentUser.getId());
        assetWarranty.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AssetWarranty.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(assetWarrantyVo.getId())) {
            criteria.andIn("id", Arrays.asList(assetWarrantyVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        assetWarrantyDao.updateByExampleSelective(assetWarranty, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param assetWarrantyVo 参数接受对象
     * @param currentUser     当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-7-8 15:03:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AssetWarrantyVo assetWarrantyVo, SysUser currentUser) {
        //筛选条件
        Example example = new Example(AssetWarranty.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(assetWarrantyVo.getId())) {
            criteria.andIn("id", Arrays.asList(assetWarrantyVo.getId().split(",")));
        }
        assetWarrantyDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 数据导出
     *
     * @param response        响应对象
     * @param assetWarrantyVo 接受参数对象
     * @author rong yuan
     * @date 2019/7/11 15:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportassetWarranty(HttpServletResponse response, AssetWarrantyVo assetWarrantyVo) {
        List<AssetWarranty> list = findList(assetWarrantyVo);
        EasyPoiUtil.exportExcel(list, "资产报修信息", "资产报修信息", AssetWarranty.class, "资产报修信息.xls", response);
    }

    /**
     * 资产报修按月统计
     *
     * @param assetWarrantyVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-08-21 11:51
     */
    @Override
    public ReturnResult statisticsByMonth(AssetWarrantyVo assetWarrantyVo) {
        List<String> monthList = new ArrayList<>();
        List<Integer> assetNumList = new ArrayList<>();
        String year = String.valueOf(DateUtil.thisYear());
        for (int i = 1; i <= 12; i++) {
            monthList.add(i + "月份");
            if (i < 10) {
                assetWarrantyVo.setCreateTime(year + "-0" + i);
            } else {
                assetWarrantyVo.setCreateTime(year + "-" + i);
            }
            List<AssetWarranty> assetWarrantyList = this.findList(assetWarrantyVo);
            assetNumList.add(assetWarrantyList == null ? 0 : assetWarrantyList.size());
        }
        List<Object> result = new ArrayList<>();
        result.add(monthList);
        result.add(assetNumList);
        return new ReturnResult(result);
    }
}